package com.heima.wemedia.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.file.service.FileStorageService;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmUserService;
import com.heima.wemedia.utils.WmThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Resource
    private FileStorageService fileStorageService;

    @Override
    @Transactional
    public ResponseResult<WmMaterial> uploadPicture(MultipartFile multipartFile) {

//        拿到图片进行上传
        String postFix = StrUtil.subAfter(multipartFile.getOriginalFilename(), ".", true);
        String fileName = StrUtil.format("{}.{}", IdUtil.simpleUUID(), postFix);
        try {
            String imageUrl = this.fileStorageService.uploadHtmlFile(null, fileName, multipartFile.getInputStream());

//            保存到数据库里面
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(imageUrl);
            wmMaterial.setType(Convert.toShort(0));
            wmMaterial.setUserId(WmThreadLocalUtil.get().getId());
            wmMaterial.setIsCollection(Convert.toShort(0));
            wmMaterial.setCreatedTime(new Date());
            super.save(wmMaterial);
            return  ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public ResponseResult<List<WmMaterial>> queryList(WmMaterialDto wmMaterialDto) {
//        1,检查参数
        wmMaterialDto.checkParam();
//        分页参数
        IPage<WmMaterial> page = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
//        构造条件
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.lambdaQuery();

//       设置条件。
        LambdaQueryWrapper<WmMaterial> wrapper1 = wrapper.eq(ObjectUtil.equal(wmMaterialDto.getIsCollection(), wmMaterialDto.getIsCollection()), WmMaterial::getIsCollection, wmMaterialDto.getIsCollection())
                .eq(WmMaterial::getUserId, WmThreadLocalUtil.get().getId())
                .orderByDesc(WmMaterial::getCreatedTime);
//        进行查询数据
        IPage<WmMaterial> page1 = super.page(page, wrapper1);
        return PageResponseResult.<List<WmMaterial>>builder()
                .currentPage(wmMaterialDto.getPage())
                .size(wmMaterialDto.getSize())
                .total(Convert.toInt(page1.getTotal()))
                .data(page1.getRecords())
                .build();
    }
}
